<template>

  <smart-page>
    <template #actions>
      <a-button v-auth="Acl.SYSTEM_CONFIG_REFRESH" :disabled="selectedKeys.length === 0" @click="onRefresh()">刷新缓存</a-button>
      <a-button v-auth="Acl.SYSTEM_CONFIG_DELETE" :disabled="selectedKeys.length === 0" @click="onDelete()">批量删除</a-button>
      <a-button v-auth="Acl.SYSTEM_CONFIG_ADD" type="primary" @click="editCtr.open()">新建配置</a-button>
    </template>
    <template #search>
      <smart-search :query-array="CONFIG_SEARCH" @on-handler="onSearch"/>
    </template>
    <a-table v-model:selectedKeys="selectedKeys"
             :table-layout-fixed="theme.layoutFixed"
             :row-selection="rowSelection"
             :columns="CONFIG_COLUMNS"
             :row-key="rowKey"
             :column-resizable="theme.resizable"
             :bordered="theme.bordered"
             :stripe="theme.stripe"
             :sticky-header="theme.sticky"
             :hoverable="theme.hoverAble"
             :pagination="pagination"
             :loading="theme.loading"
             :data="dataSource"
             @page-change="onPageChange"
             @page-size-change="onPageSizeChange"
    >

      <template #action="{ record }">
        <a-space>
          <a-link @click="readCtr.open(record)"> 查看</a-link>
          <a-link v-auth="Acl.SYSTEM_CONFIG_UPDATE" @click="editCtr.open(record)"> 编辑</a-link>
          <a-link v-auth="Acl.SYSTEM_CONFIG_DELETE" status="danger" @click="onDelete(record)"> 删除</a-link>
        </a-space>
      </template>
    </a-table>


    <edit-modal v-if="editCtr.show" :record="editCtr.record" @cancel="editCtr.close"/>
    <read-drawer v-if="readCtr.show" :record="readCtr.record" @cancel="readCtr.close"/>
  </smart-page>


</template>
<script lang="ts" setup name="system-settings-config">
import {onMounted} from 'vue';

import {CONFIG_SEARCH} from '../resource/search'
import {CONFIG_COLUMNS} from "../resource/colums";
import {TablePage} from "@/libs/core";
import {SystemHttp} from "../resource/http";
import EditModal from './edit.vue'
import ReadDrawer from './read.vue'
import {MsgUtil} from "@/utils";
import {Acl} from "@/libs/acl";

const {getConfigPage, delConfig, refreshConfig} = SystemHttp
const pageConfig = {title: '系统配置', rowKey: 'id', nameKey: 'title'}
const tablePage = new TablePage(pageConfig).setFetchXhr(getConfigPage)
    .setPermission(Acl.SYSTEM_CONFIG_PAGE_LIST)
    .setDeleteXhr(delConfig);


const {theme, pagination, rowKey, dataSource, rowSelection, selectedKeys, editCtr, readCtr} = tablePage
const {onFetch, onPageChange, onPageSizeChange, onDelete, onSearch} = tablePage
onMounted(() => onFetch())


const onRefresh = () => {

  const handler = async () => {
    const message = await refreshConfig(selectedKeys.value)
    MsgUtil.notify("操作成功", message);
  }
  MsgUtil.confirm("确定要刷新选中配置的缓存信息吗?", handler)
}
</script>

